package com.example.shushiyunsst.widget.verification.view;

import android.content.Context;
import android.text.TextUtils;
import android.util.AttributeSet;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.EditText;

import com.example.shushiyunsst.R;
import com.example.shushiyunsst.widget.verification.manager.VerificationManager;

import java.util.List;

/**
 * Created by zhoulikai on 17/4/21.
 * 图片验证码添加
 */

public class ImageVerificationView extends BaseVerificationView implements View.OnClickListener {

    protected EditText checkEt;
    protected CheckView checkView;
    protected Button submitBtn;
    protected Button cancelBtn;
    protected TipTextView tipTv;
    protected VerificationListener listener;
    private ICheckCode checkCodeGenerator;
    private final static String TAG = "ImageVerificationView";
    private VerificationManager.VerificationManagerParameter verificationManagerParameter;

    public ImageVerificationView(Context context) {
        super(context);
    }

    public void setVerificationManagerParameter(VerificationManager.VerificationManagerParameter verificationManagerParameter) {
        this.verificationManagerParameter = verificationManagerParameter;
        if (this.tipTv != null) {
            this.tipTv.setVerificationManagerParameter(verificationManagerParameter);
        }
    }

    @Override
    protected int setVerificationViewId() {
        return R.layout.image_verification_layout;
    }

    @Override
    protected void initView(Context context, AttributeSet attrs) {
        checkCodeGenerator = new CheckCodeGenerator();
        checkEt = (EditText) this.findViewById(R.id.check_et);
        checkView = (CheckView) this.findViewById(R.id.check_view);
        submitBtn = (Button) this.findViewById(R.id.submit_btn);
        tipTv = (TipTextView) this.findViewById(R.id.tip_tv);
        cancelBtn = (Button) this.findViewById(R.id.cancel_btn);

        checkView.setOnClickListener(this);
        submitBtn.setOnClickListener(this);
        cancelBtn.setOnClickListener(this);
    }

    @Override
    public void setData(ViewGroup view, List<String> imageList, VerificationListener listener) {
        this.listener = listener;
        refreshCheckCode();
    }

    protected void verification() {
        String code = checkEt.getText().toString();
        if (checkView.verification(code)) {
            if (this.listener != null) {
                this.listener.notifyStatusResult(VerificationListener.SUCCESS_STATUS);

                if (!TextUtils.isEmpty(code) && code.length() == 4) {
                    this.listener.notifyStatusResult(VerificationListener.RESULT_SUCCESS);
                }
            }
            tipTv.show(TipTextView.CheckStatus.SUCCESS);
        } else {
            if (this.listener != null) {
                this.listener.notifyStatusResult(VerificationListener.FAIL_STATUS);

                if (!TextUtils.isEmpty(code) && code.length() == 4) {
                    this.listener.notifyStatusResult(VerificationListener.RESULT_FAIL);
                }
            }
            tipTv.show(TipTextView.CheckStatus.FAIL);
        }
    }

    protected void refreshCheckCode() {
        checkEt.setText("");
        tipTv.show(TipTextView.CheckStatus.START);
        checkView.makeCheckCode(checkCodeGenerator);
        if (this.listener != null) {
            this.listener.notifyStatusResult(VerificationListener.START_CHECK_STATUS);
        }
    }

    protected void cancelVerification() {
        if (this.listener != null) {
            this.listener.notifyStatusResult(VerificationListener.CACEL_STATUS);
        }
    }

    @Override
    public void onClick(View v) {
        int id = v.getId();
        if (id == R.id.submit_btn) {
            verification();
        } else if (id == R.id.check_view) {
            refreshCheckCode();
        } else if (id == R.id.cancel_btn) {
            cancelVerification();
        }
    }
}